package com.beantools.reflect.property;

import com.alibaba.fastjson.JSONObject;
import com.beantools.util.ArrayUtil;
import com.beantools.util.CollectionUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.Set;

import static com.beantools.constants.MethodPrefix.PREFIX_GET;
import static com.beantools.constants.MethodPrefix.PREFIX_IS;
import static com.beantools.constants.SingleChar.SERIES_COMMA_OP;
import static com.beantools.util.StringUtil.isBlank;
import static java.math.BigDecimal.*;

/**
 * Created By zby on 23:15 2019/6/16
 */

public class JsonInvoker implements InvocationHandler {


    public JSONObject getJsons(String classNames, String packagePath, String... methodName) {
        if (isBlank(classNames)) {
            return null;
        }
        JSONObject body = new JSONObject();
        if (classNames.contains(SERIES_COMMA_OP)) {
            String[] paths = classNames.split(SERIES_COMMA_OP);
            for (String var : paths) {
                Set set = getObject(var, packagePath, methodName);
                if (CollectionUtil.isEmpty(set)) {
                    continue;
                }
                body.put(var, set);
            }
            return body;
        }
        Set set = getObject(classNames, packagePath, methodName);
        if (CollectionUtil.isEmpty(set)) {
            return null;
        }
        body.put(classNames, getObject(classNames, packagePath, methodName));
        return body;
    }

    /**
     * Created By zby on 19:05 2019/5/30
     *
     * @param className 文件路径
     */
    public static Set getObject(String className, String packegePath, String... methodName) {
        if (isBlank(className) || ArrayUtil.isEmpty(methodName)) {
            return null;
        }
        className = packegePath + className;
        try {
            Class<?> clazz = Class.forName(className);
            Object[] var = clazz.getEnumConstants();
            Set var1 = new LinkedHashSet(var.length);
            for (Object o : var) {
                var1.add(getJsonObject(o, methodName));
            }
            return var1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Created By zby on 20:41 2019/5/30
     * 将枚举的值以key value的方式填充
     */
    public static JSONObject getJsonObject(Object o, String... methodNames) {
        JSONObject json = null;
        if (o == null || ArrayUtil.isEmpty(methodNames)) {
            return json;
        }
        json = new JSONObject();
        json.put("key", o);
        Class var = o.getClass();
        for (String methodName : methodNames) {
            try {
                if (isBlank(methodName)) {
                    continue;
                }
                String var1;
                if (methodName.startsWith(PREFIX_GET)) {
                    var1 = methodName.substring(ROUND_FLOOR);

                } else if (methodName.startsWith(PREFIX_IS)) {
                    var1 = methodName.substring(ROUND_CEILING);
                } else {
                    throw new RuntimeException("方法名错误，只能以set或is开头【methodName】=" + methodName);
                }
                var1 = var1.substring(ROUND_UP, ROUND_DOWN).toLowerCase() + var1.substring(ROUND_DOWN);
                json.put(var1, var.getDeclaredMethod(methodName).invoke(o, null));
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return json;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return null;
    }
}
